using System;
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json;
using ycx;

namespace RFS.Domain.PaiShi.Zuhuo
{
    public class ZuhuaRecord
    {
        public ulong Id { get; set; } = 1.NewSnowflakeId();
        public ZuHuoType ZuHuoType { get; set; }
        public DateTime ZHTime { get; set; } = DateTime.Now;
        public string 发货单码 { get; set; } = "";//
        public string 拍市品种码 { get; set; } = "";//

        public IList<ZuhuaZiLingItem> FaHuoZiLingItems { get; set; } = new List<ZuhuaZiLingItem>();

        public int 枝数
        {
            get
            {
                var gg = 10;
                if (!string.IsNullOrEmpty(ProdDemo.规格))
                    1.TryRun(() => gg = int.Parse(ProdDemo.规格));
                return FaHuoZiLingItems.Count() * gg;
            }
        }

        public ZaHuaItem ProdDemo
        {
            get
            {
                var r = FaHuoZiLingItems.FirstOrDefault();
                if (r == default || r.ZaHuaItem == null) return new ZaHuaItem();
                return r.ZaHuaItem;
            }
        }

        public DateTime CreateTime { get; set; } = DateTime.Now;
        public int 批次 { get; set; }
        public string 等级 { get; set; } = "";
        public string 规格 { get; set; } = "";
        public string 拍市品种名 { get; set; }//
        public string 长度 { get; set; }//
        public string 复核人 { get; set; }//
        public string 录单人 { get; set; }//
        public string 供货商码 { get; set; }//
        public string 地块 { get; set; }//
        public int 长度基批次 { get; set; }

        public void addItem(ZuhuaZiLingItem o)
        {
            if (!FaHuoZiLingItems.Contains(o))
            {
                FaHuoZiLingItems.Add(o);
                o.ParentId = this.Id;
            }
        }

        public void addZahuas(List<ZaHuaItem> zas)
        {

            var ns=zas.Where(x => x.ZhiLingItemId == 0 && FaHuoZiLingItems.FirstOrDefault(y => y.Id == x.Id) == null);
           var res=  ns.Select(x => new ZuhuaZiLingItem()
            {
                Id = x.Id,
                ParentId = this.Id,
                ZaHuaItem = x,
                ZaHuaItemId = x.Id

            });
            
           foreach (var z in res)
           {
               addItem(z);
           }
           

        }

        public void RemoveZahuas(List<ZaHuaItem> os)
        {
            var its = this.FaHuoZiLingItems
                .Where(x => os.FirstOrDefault(y => y.Id == x.ZaHuaItemId) != null)
                .ToList() ;


            foreach (var z in its)
            {
                
                    FaHuoZiLingItems.Remove(z);
            }


        }
    }
}